﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AmiEmuLib.Configuration.AmigaEventsConfig;
using TomanuExtensions;

namespace AmiEmuLib.Configuration.KeyboardConfig
{
    /// <summary>
    /// List of raw codes for all amiga models.
    /// </summary>
    public static class RawCodesList
    {
        /// <summary>
        /// List of raw codes for all amiga models.
        /// </summary>
        private static List<RawCodes> m_list = new List<RawCodes>();

        /// <summary>
        /// Static constructor.
        /// </summary>
        static RawCodesList()
        {
            using (Stream stream = StreamExtensions.FromResource(typeof(RawCodesList),
                "RawCodesList.xml"))
            {
                XmlReaderExtensions.ReadXml(stream, (reader) =>
                {
                    reader.ReadStartElement(typeof(RawCodesList).Name);

                    while (reader.IsStartElement())
                        m_list.Add(new RawCodes(reader));

                    reader.ReadEndElement();
                });
            }
        }

        /// <summary>
        /// List of raw codes for all amiga models.
        /// </summary>
        public static IEnumerable<RawCodes> List
        {
            get
            {
                return m_list;
            }
        }

        /// <summary>
        /// For development use only.
        /// </summary>
        public static void SaveToXml()
        {
            XmlWriterExtensions.WriteXml("RawCodesList.xml", (writer) =>
            {
                writer.WriteStartElement(typeof(RawCodesList).Name);

                foreach (var raw_codes in m_list)
                    raw_codes.WriteXml(writer);

                writer.WriteEndElement();
            });
        }
    }
}
